﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 空运国内联程信息
    /// </summary>
    public class 空运国内网络联程
    {
        public class Pos
        {
            public string Name { get; set; }
            public decimal Lng { get; set; }
            public decimal Lat { get; set; }
        }
        public class Line
        {
            public Pos From { get; set; }
            public Pos To { get; set; }
            public double Value { get; set; }
        }

        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder nameBuilder = new StringBuilder();
            StringBuilder valueBuilder = new StringBuilder();
            List<Pos> names = new List<Pos>();
            List<Line> lines = new List<Line>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string fromString = Convert.ToString(dataTable.Rows[index][2]).Trim();
                string toString = Convert.ToString(dataTable.Rows[index][3]).Trim();
                string fromGeoString = Convert.ToString(dataTable.Rows[index][7]).Trim().Replace("，", ",");
                string toGeoString = Convert.ToString(dataTable.Rows[index][8]).Trim().Replace("，", ",");
                string valueString = Convert.ToString(dataTable.Rows[index][5]).Trim();

                if (string.IsNullOrEmpty(fromString)
                    || string.IsNullOrEmpty(toString)
                    || string.IsNullOrEmpty(fromGeoString)
                    || string.IsNullOrEmpty(toGeoString))
                    continue;

                decimal fromLng = 0;
                decimal fromLat = 0;
                decimal toLng = 0;
                decimal toLat = 0;

                double value = 0;

                string[] fromGeoArr = fromGeoString.Split(new char[] { ',' });
                string[] toGeoArr = toGeoString.Split(new char[] { ',' });

                if (!decimal.TryParse(fromGeoArr[0], out fromLng)
                    || !decimal.TryParse(fromGeoArr[1], out fromLat)
                    || !decimal.TryParse(toGeoArr[0], out toLng)
                    || !decimal.TryParse(toGeoArr[1], out toLat))
                {
                    continue;
                }

                if (double.TryParse(valueString, out value) == false)
                    continue;

                Pos fromPos = null;
                Pos toPos = null;
                bool isInsert = false;
                for (int nameIndex = 0; nameIndex < names.Count; nameIndex++)
                {
                    if (string.Equals(names[nameIndex].Name, fromString))
                    {
                        fromPos = names[nameIndex];
                        isInsert = true;
                        break;
                    }
                }
                if (isInsert == false)
                {
                    fromPos = new Pos()
                    {
                        Name = fromString,
                        Lng = fromLng,
                        Lat = fromLat
                    };
                    names.Add(fromPos);
                }
                isInsert = false;
                for (int nameIndex = 0; nameIndex < names.Count; nameIndex++)
                {
                    if (string.Equals(names[nameIndex].Name, toString))
                    {
                        toPos = names[nameIndex];
                        isInsert = true;
                        break;
                    }
                }
                if (isInsert == false)
                {
                    toPos = new Pos()
                    {
                        Name = toString,
                        Lng = toLng,
                        Lat = toLat
                    };
                    names.Add(toPos);
                }

                isInsert = false;
                for (int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
                {
                    if (lines[lineIndex].From == fromPos && lines[lineIndex].To == toPos)
                    {
                        lines[lineIndex].Value += value;
                        isInsert = true;
                    }
                }

                if (isInsert == false)
                {
                    Line line = new Line()
                    {
                        From = fromPos,
                        To = toPos
                    };
                    line.Value = value;
                    lines.Add(line);
                }
            }


            StringBuilder posBuilder = new StringBuilder();
            StringBuilder lineBuilder = new StringBuilder();
            for (var index = 0; index < names.Count; index++)
            {
                if (index == 0)
                {
                    nameBuilder.AppendFormat("\"{0}\":[{1},{2}]", names[index].Name, names[index].Lng, names[index].Lat);
                }
                else
                {
                    nameBuilder.AppendFormat(",\"{0}\":[{1},{2}]", names[index].Name, names[index].Lng, names[index].Lat);
                }
            }
            for (int index = 0; index < lines.Count; index++)
            {
                if (index == 0)
                {
                    lineBuilder.AppendFormat("[{{\"name\":\"{0}\"}},{{\"name\":\"{1}\",\"value\":{2}}}]", lines[index].From.Name, lines[index].To.Name, lines[index].Value);
                }
                else
                {
                    lineBuilder.AppendFormat(",[{{\"name\":\"{0}\"}},{{\"name\":\"{1}\",\"value\":{2}}}]", lines[index].From.Name, lines[index].To.Name, lines[index].Value);
                }
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"coordMap\":{{{0}}},\"links\":[{1}]}}", nameBuilder, lineBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}